// userController
// user业务逻辑
const { User } = require('../model/user')
const bcrypt = require('bcrypt')

// 注册用户
exports.register = async (req, res, next) => {
    // 进行错误捕获---》捕获书写业务逻辑时候得到错误
    try {
        let { email, username, password } = req.validValue
        // 1、根据邮箱查询该用户是否已经被注册
        let user = await User.findOne({ email })
        //  
        if (user) {
            return res.status(400).json({
                code: 400,
                msg: '该邮箱已被注册，请重新输入！',
                data: { email }
            })
        }
        // 3、若未注册，并返回成功结果
        // 3.1 加密
        const salt = await bcrypt.genSalt(10)
        password = bcrypt.hashSync(password, salt)

        // 3.2 存储在数据库
        user = new User({
            email,
            username,
            password
        })

        let result = await user.save();
        console.log('注册结果', result);
        res.status(200).json({
            code: 200,
            mas: '注册成功',
            data: { email }
        })
    } catch (error) {
        next(error)
    }
}

// 获取全部用户
exports.getUserList = async (req, res, next) => {
    try {
        let userList = await User.find()
        if (!userList) {
            return res.status(400).json({
                code: 400,
                msg: '用户列表不存在！'
            })
        }
        res.status(200).json({
            code: 200,
            msg: '用户列表查询成功',
            data: { userList }
        })
    } catch (error) {
        next(error)
    }
}

// 查询用户
exports.getUser = async (req, res, next) => {
    try {
        const uid = req.params.id
        let user = await User.findById(uid)
        if (!user) {
            return res.status(400).json({
                code: 400,
                msg: '用户不存在！'
            })
        }
        res.status(200).json({
            code: 200,
            msg: '用户列询成功',
            data: { user }
        })
    } catch (error) {
        next(error)
    }
}

// 修改用户信息
exports.updateUser = async (req, res, next) => {
    try {
        let uid = req.params.id
        let user = req.body
        console.log("user:", user);
        let data = await User.findByIdAndUpdate(uid, user)
        if (!data) {
            return res.status(400).json({
                code: 400,
                msg: '用户修改信息失败！'
            })
        }
        res.status(200).json({
            code: 200,
            msg: '用户修改成功',
            data: { user }
        })
        console.log(data);
        // let user = await User
        res.send('修改用户信息')
    } catch (error) {
        next(error)
    }
}

// 删除用户信息
exports.delUser = async (req, res, next) => {
    try {
        // res.send('删除用户信息')
        let uid = req.params.id
        let result = await User.findByIdAndDelete(uid)
        if (!result) {
            return res.status(400).json({
                code: 400,
                msg: '删除失败！',
                value: {
                    __id: uid
                }
            })
        }
        let body = req.body
        res.status(200).json({
            code: 200,
            msg: '用户删除成功！',
            data: {
                body
            }
        })
    } catch (error) {
        next(error)
    }
}